<?php

namespace App\Controller\Admin;

use App\Controller\Example\FundsExample;
use App\Controller\Example\UserExample;
use App\Controller\Example\WithdrawExample;
use App\Models\AdminModel;
use App\Models\Conn;
use App\Models\FundsModel;
use App\Models\UsersCollModel;
use App\Models\UsersModel;
use App\Models\WithdrawModel;
use Core\Common\Common;
use Core\Enums\ErrorEnum;
use Core\Enums\FundsEnum;
use Core\Enums\FundsGenreEnum;
use Core\Result\Result;

class Withdraw
{
    /**
     * 管理后台提现列表
     * entry token,uid,serial,type,status,startTime,endTime,page,limit
     * return json
     * */
    public function withdrawList($data){
        $assemble = Common::assemble(new WithdrawExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $joinSql = " WHERE 1";
        if($assemble->uid !=null){ $joinSql.=" and `uid` ='$assemble->uid' ";}
        if($assemble->serial !=null){ $joinSql.=" and `serial` ='$assemble->serial' ";}
        if($assemble->type !=null){ $joinSql.=" and `type` ='$assemble->type' ";}
        if($assemble->status !=null){ $joinSql.=" and `status` ='$assemble->status' ";}
        if($assemble->startTime !=null){$assemble->startTime = strtotime($assemble->startTime); $joinSql.=" and UNIX_TIMESTAMP(`time`) >= '$assemble->startTime' ";}
        if($assemble->endTime !=null){$assemble->endTime = strtotime($assemble->endTime); $joinSql.=" and UNIX_TIMESTAMP(`time`) <= '$assemble->endTime' ";}
        $sql = "SELECT `id`,`uid`,`serial`,`money`,`amount`,`charge`,`type`,`time`,`stime`,`status`,`cause`,`genre` FROM `withdraw` $joinSql order by id desc limit $identical->pageStart,$identical->limit";
        $readConn = $conn->readConn($sql,true);
        foreach ($readConn as $item=>$value){
            $readConn[$item]["info"] = UsersCollModel::getUsersCollInside($conn, $value["uid"]);
            $readConn[$item]["tixianren"] = UsersModel::getUserNameInsideId($conn, $value["uid"]);
        }
        $returnData["list"]=$readConn;
        $countSql="SELECT count(id) as count FROM `withdraw` $joinSql";
        $totalSql="SELECT sum(money) as 总提现金额,sum(amount) as 总到账金额,sum(charge) as 总手续费 FROM `withdraw` $joinSql limit $identical->pageStart,$identical->limit";
        $returnData["count"] = $conn->readConn($countSql)["count"];
        $returnData["total"] = $conn->readConn($totalSql);
        Result::success($returnData);
    }
    /**
     * 管理后台编辑提现
     * entry token,id,status,cause
     * return json
     * */
    public function editWithdraw($data){
        $assemble = Common::assemble(new WithdrawExample(), $data);
        $conn = new Conn();
        $adminInToken = AdminModel::getAdminInToken($conn, $assemble->token);
        $conn->shiwuStart();
        if($assemble->status == -1){
            //提现驳回
            self::bohui($conn,$assemble);
            AdminLog::addAdminLog($adminInToken["username"],"提现列表","驳回提现:[".$assemble->id."]");
        }else{
            AdminLog::addAdminLog($adminInToken["username"],"提现列表","审核提现:[".$assemble->id."]");
        }
        if(WithdrawModel::editWithdrawInside($conn,$assemble) !=1){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SUBMITFAILED);
        }
        $conn->shiwuEnd();
        Result::success("ok");
    }
    /**
     * 管理后台提现驳回
     * entry token,id,status,cause
     * return json
     * */
    public function bohui($conn,$withdrawExample){
        $userWithdrawListInside = WithdrawModel::getUserWithdrawInsideInid($conn, $withdrawExample->id);
        if($userWithdrawListInside["status"] != 1){
            Result::error(ErrorEnum::INVALIDORDER);
        }
        $userExample = Common::assemble(new UserExample(),UsersModel::getUserInsideId($conn, $userWithdrawListInside["uid"]));
        $fundsExample = new FundsExample();
        $fundsExample->uid = $userExample->id;
        $fundsExample->number = $userWithdrawListInside["serial"];
        $fundsExample->type = FundsEnum::WITHDRAWALREJECTION;
        $fundsExample->amount = $userWithdrawListInside["money"];
        $fundsExample->balance = $userExample->money + $userWithdrawListInside["money"];
        $fundsExample->genre = FundsGenreEnum::MONEY;
        if(UsersModel::editUserMoneyAddInside($conn,$userWithdrawListInside["money"],$userExample) !=1 ||
            FundsModel::insertFunds($conn, $fundsExample) != 1
        ){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::SUBMITFAILED);
        }
    }
}